package com.smilive.delay.queue.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @program: delay_queue_start
 * @Date: 2022-02-09 15:52
 * @Author: smilive
 * @Description: 死信队列消费者
 */
@Slf4j
@Component
@RabbitListener(queues = RabbitMqBaseConfig.DEAL_QUEUE)
public class DealQueueConsumer {

    private static final DateTimeFormatter F = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @RabbitHandler
    public void receive(RabbitmqDelayTask task, Channel channel, Message message) throws IOException {

        log.info("任务执行时间:{},任务创建时间:{},任务ID:{}",
                LocalDateTime.now().format(F),
                LocalDateTime.ofInstant(Instant.ofEpochMilli(task.getTimestamp()), ZoneId.systemDefault()).format(F),
                task.getTaskId());
        // 手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
